package com.atmilan.quartz.utils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.CollectionUtils;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 通用转换类
 * @author muyu
 */
@Slf4j
public class BeanConvert<D,E> {

    public static <E ,D > E copyBean(D entity,Class cls){
        if(entity==null) {
            return null;
        }
        try {
            Object dto = cls.newInstance();
            BeanUtil.copyProperties(entity,dto);
            return (E)dto;
        } catch (InstantiationException e) {
            log.error("convert bean InstantiationException! entity={}",entity.toString(),e);
        } catch (IllegalAccessException e) {
            log.error("convert bean IllegalAccessException! entity={}",entity.toString(),e);
        }
        return null;
    }

    public static <E,D> List<E> copyBeanList(List<D> list,Class cls){
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<E>();
        }
        List<E> li = new ArrayList<E>();
        for(D c:list){
            E e = copyBean(c,cls);
            li.add(e);
        }
        return li;
    }
}
